package com.senthink.gl.business.api.controller;

import com.senthink.gl.business.area.AreaRequest;
import com.senthink.gl.business.area.AreaService;
import com.senthink.gl.business.common.entity.PageDto;
import com.senthink.gl.business.common.entity.SearchRequest;
import com.senthink.gl.business.response.ApiResponse;
import com.senthink.gl.business.security.JwtTokenUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 区域管理Controller
 *
 * @author zooqi@senthink.com
 * @date 2017/11/22
 */
@Api(tags = "区域管理Controller")
@RestController
public class AreaController {

    @Autowired
    private AreaService service;
    @Autowired
    private JwtTokenUtils jwtTokenUtils;
    //顺舟四代网关指令发送

    /**
     * 获取区域详情
     *
     * @param areaRequest
     * @return
     */
    @ApiOperation(notes = "获取区域详情",value = "获取区域详情")
    @RequestMapping(value = "${uri.area.area}", method = RequestMethod.GET)
    public ResponseEntity<?> getArea(@Validated(AreaRequest.FindOneGroup.class) AreaRequest areaRequest) {
        return ResponseEntity.ok(service.getDetailByAid(areaRequest));
    }

    /**
     * 添加区域
     *
     * @param areaRequest
     * @return
     */
    @ApiOperation(notes = "添加区域",value = "添加区域")
    @RequestMapping(value = "${uri.area.area}", method = RequestMethod.POST)
    public ResponseEntity<?> postArea(@RequestBody @Validated(AreaRequest.SaveOneGroup.class) AreaRequest areaRequest,@RequestHeader("${jwt.header}") String authorization) {
        return ResponseEntity.ok(service.save(areaRequest));
    }

    /**
     * 删除区域
     *
     * @param areaRequest
     * @return
     */
    @ApiOperation(notes = "删除区域",value = "删除区域")
    @RequestMapping(value = "${uri.area.area}", method = RequestMethod.DELETE)
    public ResponseEntity<?> deleteArea(@Validated(AreaRequest.DeleteOneGroup.class) AreaRequest areaRequest) {
        return ResponseEntity.ok(service.delete(areaRequest));
    }

    /**
     * 修改区域信息
     *
     * @param areaRequest
     * @return
     */
    @ApiOperation(notes = "修改区域信息",value = "修改区域信息")
    @RequestMapping(value = "${uri.area.area}", method = RequestMethod.PUT)
    public ResponseEntity<?> putArea(@RequestBody @Validated(AreaRequest.UpdateOneGroup.class) AreaRequest areaRequest) {
        return ResponseEntity.ok(service.update(areaRequest));
    }

    /**
     * 分页、筛选区域列表
     *
     * @param pageDTO
     * @param searchRequest
     * @return
     */
    @ApiOperation(notes = "分页、筛选区域列表",value = "分页、筛选区域列表")
    @RequestMapping(value = "${uri.area.areas}", method = RequestMethod.GET)
    public ResponseEntity<?> areas(@Valid PageDto pageDTO, SearchRequest searchRequest) {
        ApiResponse apiResponse = service.findAll(pageDTO, searchRequest);
        return ResponseEntity.ok(apiResponse);
    }

    /**
     * 获取区域列表(不分页、不筛选、按名称排序)
     *
     * @return
     */
    @ApiOperation(notes = "获取区域列表(不分页、不筛选、按名称排序)",value = "获取区域列表(不分页、不筛选、按名称排序)")
    @RequestMapping(value = "${uri.area.areasnopaging}", method = RequestMethod.GET)
    public ResponseEntity<?> areasNoPaging(@RequestHeader("${jwt.header}") String authorization) {
        String account = jwtTokenUtils.getUsernameFromAuthorizationHeader(authorization);
        ApiResponse apiResponse = service.findAll(account);
        return ResponseEntity.ok(apiResponse);
    }

}
